<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">






  
  
  <link rel="stylesheet" media="all" href="/lib/Han/dist/han.min.css?v=3.3">




<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/img/icon.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/img/icon.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="cayun's blog, IT, ACM/ICPC, blog, cayun" />










<meta property="og:type" content="website">
<meta property="og:title" content="cayun&#39;s blog">
<meta property="og:url" content="http://localhost/page/10/index.html">
<meta property="og:site_name" content="cayun&#39;s blog">
<meta property="og:locale" content="zh-Hans">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="cayun&#39;s blog">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.3',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://localhost/page/10/"/>





  <title>cayun's blog</title>
  




<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-93052608-1', 'auto');
  ga('send', 'pageview');
</script>


  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?1ca7c9c8011b37b0c209724dc77c24db";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left 
  page-home">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">cayun's blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">不积跬步, 无以至千里!</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
  <section id="posts" class="posts-expand">
    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/ural1000/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/ural1000/" itemprop="url">ural 1000 A+B Problem</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-17T00:00:00+08:00">
                2017-02-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/ural1000/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/ural1000/" class="cy_cmt_count" data-xid="ural1000/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;  </div><div class="line">    <span class="keyword">int</span> a, b; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;a, &amp;b);  </div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, a + b);  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/ural1001/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/ural1001/" itemprop="url">ural 1001 Reverse Root (递归)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-17T00:00:00+08:00">
                2017-02-17
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/ural1001/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/ural1001/" class="cy_cmt_count" data-xid="ural1001/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>读入时可以直接使用long long的读入，会自动跳过空格<br>由于输出是反方向的，这时我们便可以利用递归的特性来输出</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;bits/stdc++.h&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">Read</span><span class="params">()</span> </span>&#123;  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> x;  </div><div class="line">    <span class="keyword">if</span> (<span class="built_in">scanf</span>(<span class="string">"%lld"</span>, &amp;x) != EOF) &#123;  </div><div class="line">        Read();  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%.4f\n"</span>, <span class="built_in">sqrt</span>((<span class="keyword">double</span>)x));  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;  </div><div class="line">    Read();  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/poj2342/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/poj2342/" itemprop="url">poj 2342 Anniversary party (树形DP)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-02-09T00:00:00+08:00">
                2017-02-09
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/poj2342/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/poj2342/" class="cy_cmt_count" data-xid="poj2342/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="题意-翻译来自hust-vj"><a href="#题意-翻译来自hust-vj" class="headerlink" title="题意(翻译来自hust vj)"></a>题意(翻译来自hust vj)</h1><p>一棵树有$n$个结点，每个结点有一个权值，一个结点和其直接的父亲结点不能同时选，求按照要求所能选到的点权和的最大值</p>
<h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>树形DP</p>
<p>设$dp<em>{i,0}$表示不取$i$节点的$i$子树的最大权值和，$dp</em>{i,1}$表示取$i$节点的$i$子树的最大权值和<br>设$v$是$u$的子节点，则<br>$dp<em>{u,0} = \sum {\max {(dp</em>{v,0}, dp<em>{v,1})}}$<br>$dp</em>{u,1} = v<em>{u} + Sum(dp</em>{v,0})$<br>边界条件，即当节点$u$为叶节点时<br>$dp<em>{u,0} = 0$<br>$dp</em>{u,1} = v_{u}$</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> N = <span class="number">6010</span>;  </div><div class="line"><span class="keyword">int</span> n;  </div><div class="line"><span class="keyword">int</span> fat[N]; <span class="comment">//父节点  </span></div><div class="line"><span class="keyword">int</span> v[N]; <span class="comment">//权值  </span></div><div class="line"><span class="keyword">int</span> dp[N][<span class="number">2</span>]; <span class="comment">//dp[i][0]表示i节点不选，dp[i][1]表示i节点选  </span></div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; son[N]; <span class="comment">//子节点  </span></div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">BuildTree</span><span class="params">(<span class="keyword">int</span> u)</span> </span>&#123;  </div><div class="line">    <span class="keyword">if</span> (son[u].size() == <span class="number">0</span>) &#123; <span class="comment">//若是叶节点  </span></div><div class="line">        dp[u][<span class="number">0</span>] = <span class="number">0</span>; <span class="comment">//不取时，dp[u][0] = 0  </span></div><div class="line">        dp[u][<span class="number">1</span>] = v[u]; <span class="comment">//取时，dp[u][1] = v[u]  </span></div><div class="line">    &#125; <span class="keyword">else</span> &#123;  </div><div class="line">        dp[u][<span class="number">0</span>] = <span class="number">0</span>; <span class="comment">//不取时，dp[u][0] = 0  </span></div><div class="line">        dp[u][<span class="number">1</span>] = v[u]; <span class="comment">//取时，dp[u][1] = v[u]  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; (<span class="keyword">int</span>)son[u].size(); i++) &#123;  </div><div class="line">            <span class="keyword">int</span> v = son[u][i];  </div><div class="line">            BuildTree(v);  </div><div class="line">            dp[u][<span class="number">0</span>] += max(dp[v][<span class="number">0</span>], dp[v][<span class="number">1</span>]);  </div><div class="line">            dp[u][<span class="number">1</span>] += dp[v][<span class="number">0</span>];   </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;  </div><div class="line">    <span class="keyword">while</span> (<span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n) != EOF) &#123;  </div><div class="line">        <span class="comment">//判断是否为结束标志 0 0  </span></div><div class="line">        <span class="keyword">if</span> (n == <span class="number">0</span>) &#123;  </div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);  </div><div class="line">            <span class="keyword">if</span> (n == <span class="number">0</span>) <span class="keyword">break</span>;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;  </div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;v[i]); <span class="comment">//读入节点权值  </span></div><div class="line">            son[i].clear(); <span class="comment">//清空子节点列表  </span></div><div class="line">        &#125;  </div><div class="line">        <span class="built_in">memset</span>(fat, <span class="number">0</span>, <span class="keyword">sizeof</span>(fat)); <span class="comment">//清空父节点  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++) &#123;  </div><div class="line">            <span class="keyword">int</span> a, b; <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;a, &amp;b);  </div><div class="line">            fat[a] = b; <span class="comment">//读入父节点  </span></div><div class="line">            son[b].push_back(a); <span class="comment">//添加子节点  </span></div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="comment">//寻找根节点  </span></div><div class="line">        <span class="keyword">int</span> root = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) &#123;  </div><div class="line">            <span class="keyword">if</span> (fat[i] == <span class="number">0</span>) &#123;  </div><div class="line">                root = i;  </div><div class="line">                <span class="keyword">break</span>;  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        BuildTree(root);  </div><div class="line">  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, max(dp[root][<span class="number">0</span>], dp[root][<span class="number">1</span>]));  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/linux_1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/linux_1/" itemprop="url">linux下Mysql5.5版本的编码修改为utf-8</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-12-05T00:00:00+08:00">
                2016-12-05
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/linux_1/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/linux_1/" class="cy_cmt_count" data-xid="linux_1/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <p>今天修改MySQL编码时各种mysqld.sock文件不存在，然后最后才知道原来网上的很多教程的mysql都是老版本的配置，5.5版本开始好像已经不一样了<br>下面是Linux下5.5版本的修改编码方法<br>首先用命令<code>find / | grep my.cnf</code>查看mysql配置文件的位置<br>然后vim打开<br>在client部分添加</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">default-character-set=utf8</div></pre></td></tr></table></figure>
<p>在mysqld部分添加</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">collation-server = utf8_unicode_ci  </div><div class="line">init-connect=&apos;SET NAMES utf8&apos;  </div><div class="line">character-set-server = utf8</div></pre></td></tr></table></figure>
<p>在mysql部分添加</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">default-character-set=utf8</div></pre></td></tr></table></figure>
<p>然后保存退出<br>最后重启mysql服务(这部分有些人可能会不一样吧)</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><div class="line">service mysql restart</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/codeforces715A/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/codeforces715A/" itemprop="url">codeforces 715A Plus and Square Root (构造)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-10-28T00:00:00+08:00">
                2016-10-28
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/ACM-ICPC/" itemprop="url" rel="index">
                    <span itemprop="name">ACM/ICPC</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/codeforces715A/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/codeforces715A/" class="cy_cmt_count" data-xid="codeforces715A/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          <h1 id="题意"><a href="#题意" class="headerlink" title="题意"></a>题意</h1><p>初始数为$2$，初始等级为$1$，有如下两种操作：</p>
<ol>
<li>这个数加上当前等级$k$</li>
<li>这个数开根号，但前提是这个数是完全平方数且开完根号后为下一等级$k+1$的倍数。该操作之后等级$+1$</li>
</ol>
<p>要求对于$2, ……, n+1$等级输出从上一等级到该等级的第一个操作的次数<br>答案不唯一，输出任一一个即可</p>
<h1 id="类型"><a href="#类型" class="headerlink" title="类型"></a>类型</h1><p>数学构造题</p>
<h1 id="构造方法"><a href="#构造方法" class="headerlink" title="构造方法"></a>构造方法</h1><p>假设当前等级为k，那我们只需要第一种操作到达$(k * (k+1))^2$就可以了，然后第二种操作转换成$k * (k+1)$<br>构造过程可以很容易通过数学归纳法证明，此处简单证明一下：<br>等级$1$时需要步数$2$为特殊情况<br>设当前等级为$k(k &gt;= 2)$，则当前数字为$(k-1) * k$，那么我们一定可以通过第一步操作到达$k * k * (k+1) * (k+1)$，<br>需要次数为$k * (k+1) * (k+1)-(k-1)$</p>
          <!--noindex-->
          <div class="post-button text-center">
            <a class="btn" href="/codeforces715A/" rel="contents">
              阅读全文 &raquo;
            </a>
          </div>
          <!--/noindex-->
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu5558/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu5558/" itemprop="url">hdu 5558 Alice's Classified Message (后缀数组)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-11-14T00:00:00+08:00">
                2015-11-14
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu5558/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu5558/" class="cy_cmt_count" data-xid="hdu5558/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>后缀数组<br>首先计算出sa, rank, height数组，易知，不妨设x, y($rank<em>{x} &lt; rank</em>{y}$)，那么x与y的最长公共前缀的长度便为$\min<em>{rank</em>{x} &lt; i &lt;= rank<em>{y}} {height</em>{i}}$<br>利用这个这个性质，便可以对枚举做出很大优化：<br>假设当前位置为$p$，则可以从$rank_{p}$开始分别向两边进行枚举，当枚举到minHeight &lt; 当前$k$ 便退出<br>虽然我也不懂为什么快，但最近做了好几道，发现这种方法就是快。以下代码可以在$1s$内跑过</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="comment">//后缀数组模板  </span></div><div class="line"><span class="comment">/* </span></div><div class="line"><span class="comment">样例使用: </span></div><div class="line"><span class="comment">若原字符串为"abc" </span></div><div class="line"><span class="comment">则r = &#123;1, 2, 3, 0&#125;; </span></div><div class="line"><span class="comment">n = 3; </span></div><div class="line"><span class="comment">GetSa(r, sa, n+1, m); </span></div><div class="line"><span class="comment">GetHeight(r, rank, height, n); </span></div><div class="line"><span class="comment">其中 </span></div><div class="line"><span class="comment">r为字符串转化成的数组，末尾再添一个0 </span></div><div class="line"><span class="comment">n为字符串的长度 </span></div><div class="line"><span class="comment">sa[i]表示排名在i的后缀下标,i从1开始，满足Suffix(sa[i]) &lt; Suffix(sa[i+1]) </span></div><div class="line"><span class="comment">rank[i]表示Suffix(i)的排名，为sa的逆 </span></div><div class="line"><span class="comment">height[i]表示sa[i]和sa[i-1]的最长公共前缀 </span></div><div class="line"><span class="comment">*/</span>  </div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">SuffixArray</span>  </span></div><div class="line"><span class="class">&#123;</span>  </div><div class="line">    <span class="keyword">enum</span> &#123;MAXN = <span class="number">100010</span>&#125;;  </div><div class="line">    <span class="keyword">int</span> wa[MAXN], wb[MAXN], wsf[MAXN], wv[MAXN];  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">int</span> <span class="title">Cmp</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> a, <span class="keyword">int</span> b, <span class="keyword">int</span> k)</span>  </span></div><div class="line"><span class="function">    </span>&#123;  </div><div class="line">        <span class="keyword">return</span> r[a] == r[b] &amp;&amp; r[a+k] == r[b+k];  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">GetSa</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> n, <span class="keyword">int</span> m)</span>  </span></div><div class="line"><span class="function">    </span>&#123;  </div><div class="line">        <span class="keyword">int</span> i, j, p, *x = wa, *y = wb, *t;  </div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; m; i++)    </div><div class="line">            wsf[i] = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)    </div><div class="line">            wsf[x[i]=r[i]]++;  </div><div class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; m; i++)    </div><div class="line">            wsf[i] += wsf[i<span class="number">-1</span>];  </div><div class="line">        <span class="keyword">for</span> (i = n - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--)    </div><div class="line">            sa[--wsf[x[i]]] = i;  </div><div class="line">        p = j = <span class="number">1</span>;  </div><div class="line">        <span class="keyword">for</span> (; p &lt; n; j *= <span class="number">2</span>, m = p)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">for</span> (p = <span class="number">0</span>, i = n - j; i &lt; n; i++)  y[p++] = i;  </div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)    </div><div class="line">                <span class="keyword">if</span>(sa[i] &gt;= j)  y[p++] = sa[i] - j;  </div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)    </div><div class="line">                wv[i] = x[y[i]];  </div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; m; i++)    </div><div class="line">                wsf[i] = <span class="number">0</span>;  </div><div class="line">            <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)    </div><div class="line">                wsf[wv[i]]++;  </div><div class="line">            <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt; m; i++)    </div><div class="line">                wsf[i] += wsf[i<span class="number">-1</span>];  </div><div class="line">            <span class="keyword">for</span> (i = n - <span class="number">1</span>; i &gt;= <span class="number">0</span>; i--)    </div><div class="line">                sa[--wsf[wv[i]]] = y[i];  </div><div class="line">            swap(x, y);  </div><div class="line">            x[sa[<span class="number">0</span>]] = <span class="number">0</span>;  </div><div class="line">            <span class="keyword">for</span> (p = <span class="number">1</span>, i = <span class="number">1</span>; i &lt; n; i++)  </div><div class="line">                x[sa[i]] = Cmp(y, sa[i<span class="number">-1</span>], sa[i], j) ? p - <span class="number">1</span> : p++;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">  </div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">GetHeight</span><span class="params">(<span class="keyword">int</span> *r, <span class="keyword">int</span> *sa, <span class="keyword">int</span> *rank, <span class="keyword">int</span> *height, <span class="keyword">int</span> n)</span>  </span></div><div class="line"><span class="function">    </span>&#123;  </div><div class="line">        <span class="keyword">int</span> i, j, k = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span> (i = <span class="number">1</span>; i &lt;= n; i++)    </div><div class="line">            rank[sa[i]] = i;  </div><div class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (k) k--;  </div><div class="line">            <span class="keyword">else</span> k=<span class="number">0</span>;  </div><div class="line">            j = sa[rank[i]<span class="number">-1</span>];  </div><div class="line">            <span class="keyword">while</span> (r[i+k] == r[j+k]) k++;  </div><div class="line">            height[rank[i]] = k;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">&#125;suffixArray;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">100010</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> STSIZE = <span class="number">18</span>;  </div><div class="line"><span class="keyword">char</span> s[MAXN];  </div><div class="line"><span class="keyword">int</span> r[MAXN], sa[MAXN], rnk[MAXN], height[MAXN], pre[MAXN], nxt[MAXN];  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="comment">//input  </span></div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%s"</span>, s);  </div><div class="line">          </div><div class="line">        <span class="comment">//initialize  </span></div><div class="line">        <span class="keyword">int</span> n = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; s[i] != <span class="string">'\0'</span>; i++)  </div><div class="line">            r[n++] = s[i] - <span class="string">'a'</span> + <span class="number">1</span>;  </div><div class="line">        r[n] = <span class="number">0</span>;  </div><div class="line">          </div><div class="line">        <span class="comment">//get sa &amp; rank &amp; height  </span></div><div class="line">        suffixArray.GetSa(r, sa, n + <span class="number">1</span>, <span class="number">27</span>);  </div><div class="line">        suffixArray.GetHeight(r, sa, rnk, height, n);  </div><div class="line">          </div><div class="line">        <span class="comment">//get pre &amp; nxt  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (height[i] == <span class="number">0</span>) pre[i] = i;  </div><div class="line">            <span class="keyword">else</span> pre[i] = pre[i<span class="number">-1</span>];  </div><div class="line">        &#125;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n; i &gt; <span class="number">0</span>; i--)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (i == n || height[i+<span class="number">1</span>] == <span class="number">0</span>) nxt[i] = i;  </div><div class="line">            <span class="keyword">else</span> nxt[i] = nxt[i+<span class="number">1</span>];  </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        <span class="comment">//get answer  </span></div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case #%d:\n"</span>, kk);  </div><div class="line">        <span class="keyword">int</span> i = <span class="number">0</span>;  </div><div class="line">        <span class="keyword">while</span> (i &lt; n)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">int</span> now = rnk[i];  </div><div class="line">            <span class="keyword">int</span> left = pre[now], right = nxt[now];  </div><div class="line">            <span class="keyword">int</span> t = i, k = <span class="number">0</span>;  </div><div class="line">            <span class="keyword">int</span> minHeight = height[now];  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = now - <span class="number">1</span>; j &gt;= left; j--)  </div><div class="line">            &#123;  </div><div class="line">                minHeight = min(minHeight, height[j+<span class="number">1</span>]);  </div><div class="line">                <span class="keyword">if</span> (minHeight &lt; k) <span class="keyword">break</span>;  </div><div class="line">                <span class="keyword">if</span> (sa[j] &lt; i)  </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> ((minHeight == k &amp;&amp; sa[j] &lt; t) || minHeight &gt; k)  </div><div class="line">                    &#123;  </div><div class="line">                        t = sa[j];  </div><div class="line">                        k = minHeight;  </div><div class="line">                    &#125;  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">            <span class="keyword">if</span> (now + <span class="number">1</span> &lt;= right) minHeight = height[now+<span class="number">1</span>];  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = now + <span class="number">1</span>; j &lt;= right; j++)  </div><div class="line">            &#123;  </div><div class="line">                minHeight = min(minHeight, height[j]);  </div><div class="line">                <span class="keyword">if</span> (minHeight &lt; k) <span class="keyword">break</span>;  </div><div class="line">                <span class="keyword">if</span> (sa[j] &lt; i)   </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> ((minHeight == k &amp;&amp; sa[j] &lt; t) || minHeight &gt; k)  </div><div class="line">                    t = sa[j];  </div><div class="line">                    k = minHeight;  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">            <span class="keyword">if</span> (k == <span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">"-1 %d\n"</span>, s[i]);  </div><div class="line">            <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">"%d %d\n"</span>, k, t);  </div><div class="line">            i += (k == <span class="number">0</span> ? <span class="number">1</span> : k);  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu5550/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu5550/" itemprop="url">hdu 5550 Game Rooms (动态规划)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-11-07T00:00:00+08:00">
                2015-11-07
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu5550/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu5550/" class="cy_cmt_count" data-xid="hdu5550/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>表示制杖，参考了一下别人的题解，做完发现自己整个人都不好了，竟然没有发现这么弱的dp方程，（我不知怎么的，一开始居然想了一个$n^3$的dp方程）<br>设$dp<em>{i,s}$表示计算到第i个且将其放在该段的最后一个，这一段全为s($0$和$1$分别表示两种运动)<br>那么dp方程就是$dp</em>{i,s} = \min {dp_{j,s \oplus 1} + valueSum(j+1,i)},  j &lt; i$<br>$valueSum(i,j)$表示$[i,j]$区间内的员工到最近运动室的距离和</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">4005</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;  </div><div class="line"><span class="keyword">int</span> n;  </div><div class="line"><span class="keyword">long</span> <span class="keyword">long</span> a[<span class="number">2</span>][MAXN], sum[<span class="number">2</span>][MAXN], value[<span class="number">2</span>][MAXN], dp[MAXN][<span class="number">2</span>];  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">leftCal</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> s)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> x = value[s][r] - value[s][l<span class="number">-1</span>];  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> y = sum[s][r] - sum[s][l<span class="number">-1</span>];  </div><div class="line">    <span class="keyword">return</span> <span class="built_in">abs</span>(x - y * (l - <span class="number">1</span>));  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">rightCal</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> s)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> x = value[s][r] - value[s][l<span class="number">-1</span>];  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> y = sum[s][r] - sum[s][l<span class="number">-1</span>];  </div><div class="line">    <span class="keyword">return</span> <span class="built_in">abs</span>(x - y * (r + <span class="number">1</span>));  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">areaDist</span><span class="params">(<span class="keyword">int</span> l, <span class="keyword">int</span> r, <span class="keyword">int</span> s)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">if</span> (l == <span class="number">1</span>) <span class="keyword">return</span> rightCal(l, r, s);  </div><div class="line">    <span class="keyword">else</span> <span class="keyword">if</span> (r == n) <span class="keyword">return</span> leftCal(l, r, s);  </div><div class="line">    <span class="keyword">else</span>  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">int</span> mid = (l + r) &gt;&gt; <span class="number">1</span>;  </div><div class="line">        <span class="keyword">return</span> leftCal(l, mid, s) + rightCal(mid + <span class="number">1</span>, r, s);  </div><div class="line">    &#125;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="comment">//input  </span></div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);  </div><div class="line">        sum[<span class="number">0</span>][<span class="number">0</span>] = sum[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">0L</span>L;  </div><div class="line">        value[<span class="number">0</span>][<span class="number">0</span>] = value[<span class="number">1</span>][<span class="number">0</span>] = <span class="number">0L</span>L;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%lld%lld"</span>, &amp;a[<span class="number">0</span>][i], &amp;a[<span class="number">1</span>][i]);  </div><div class="line">            sum[<span class="number">0</span>][i] = sum[<span class="number">0</span>][i<span class="number">-1</span>] + a[<span class="number">0</span>][i];  </div><div class="line">            sum[<span class="number">1</span>][i] = sum[<span class="number">1</span>][i<span class="number">-1</span>] + a[<span class="number">1</span>][i];  </div><div class="line">            value[<span class="number">0</span>][i] = value[<span class="number">0</span>][i<span class="number">-1</span>] + a[<span class="number">0</span>][i] * i;  </div><div class="line">            value[<span class="number">1</span>][i] = value[<span class="number">1</span>][i<span class="number">-1</span>] + a[<span class="number">1</span>][i] * i;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="comment">//initialize  </span></div><div class="line">        <span class="built_in">memset</span>(dp, INF, <span class="keyword">sizeof</span>(dp));  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> s = <span class="number">0</span>; s &lt;= <span class="number">1</span>; s++)  </div><div class="line">                dp[i][s] = areaDist(<span class="number">1</span>, i, s);  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="comment">//dp  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> s = <span class="number">0</span>; s &lt;= <span class="number">1</span>; s++)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; i; j++)  </div><div class="line">                    dp[i][s] = min(dp[i][s], dp[j][s^<span class="number">1</span>] + areaDist(j + <span class="number">1</span>, i, s));  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">  </div><div class="line">        <span class="comment">//output  </span></div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case #%d: %lld\n"</span>, kk, min(dp[n][<span class="number">0</span>], dp[n][<span class="number">1</span>]));  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu5521/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu5521/" itemprop="url">hdu 5521 Meeting (最短路)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-11-02T00:00:00+08:00">
                2015-11-02
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu5521/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu5521/" class="cy_cmt_count" data-xid="hdu5521/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>最短路<br>我的构图方式是：<br>将每个集合也抽象成一个点，将集合中的每个点向集合连一条权值为$t$的有向边，集合向其中的每个点连一条权值为$0$的有向边，这样以$1$和$n$为起点各做一遍最短路。<br>枚举中间点便可以得到最后答案了。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Edge</span>  </span></div><div class="line"><span class="class">&#123;</span>  </div><div class="line">    <span class="keyword">int</span> v;  </div><div class="line">    <span class="keyword">long</span> <span class="keyword">long</span> t;  </div><div class="line">&#125;;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">100005</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXM = <span class="number">1000005</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">long</span> <span class="keyword">long</span> INF = <span class="number">1000000000000010L</span>L;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXP = MAXM + MAXN;  </div><div class="line"><span class="keyword">int</span> n, m, cnt;  </div><div class="line"><span class="keyword">int</span> pre[MAXM*<span class="number">2</span>], hd[MAXP];  </div><div class="line"><span class="keyword">bool</span> inq[MAXP];  </div><div class="line"><span class="keyword">long</span> <span class="keyword">long</span> ds[MAXP], dt[MAXP];  </div><div class="line">Edge e[MAXM*<span class="number">2</span>];  </div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ansV;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">addEdge</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v, <span class="keyword">long</span> <span class="keyword">long</span> t)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    pre[cnt] = hd[u];  </div><div class="line">    hd[u] = cnt;  </div><div class="line">    e[cnt].v = v;  </div><div class="line">    e[cnt].t = t;  </div><div class="line">    cnt++;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> kk = <span class="number">1</span>; kk &lt;= T; kk++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="comment">//input and initialize  </span></div><div class="line">        cnt = <span class="number">0</span>;  </div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d%d"</span>, &amp;n, &amp;m);  </div><div class="line">        <span class="built_in">memset</span>(hd, <span class="number">-1</span>, <span class="keyword">sizeof</span>(hd));  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= m; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">long</span> <span class="keyword">long</span> tmp;  </div><div class="line">            <span class="keyword">int</span> num;  </div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%lld%d"</span>, &amp;tmp, &amp;num);  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; num; j++)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">int</span> v; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;v);  </div><div class="line">                addEdge(v, n + i, tmp);  </div><div class="line">                addEdge(n + i, v, <span class="number">0L</span>L);  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        <span class="comment">//spfa-s  </span></div><div class="line">        <span class="built_in">memset</span>(inq, <span class="literal">false</span>, <span class="keyword">sizeof</span>(inq));  </div><div class="line">        fill(ds, ds + MAXP, INF);  </div><div class="line">        <span class="built_in">queue</span>&lt;<span class="keyword">int</span>&gt; q;  </div><div class="line">        q.push(<span class="number">1</span>); inq[<span class="number">1</span>] = <span class="literal">true</span>; ds[<span class="number">1</span>] = <span class="number">0L</span>L;  </div><div class="line">        <span class="keyword">while</span> (!q.empty())  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">int</span> u = q.front(); q.pop();  </div><div class="line">            <span class="keyword">int</span> now = hd[u];  </div><div class="line">            <span class="keyword">while</span> (now != <span class="number">-1</span>)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">const</span> Edge &amp;ee = e[now];  </div><div class="line">                <span class="keyword">if</span> (ds[u] + ee.t &lt; ds[ee.v])  </div><div class="line">                &#123;  </div><div class="line">                    ds[ee.v] = ds[u] + ee.t;  </div><div class="line">                    <span class="keyword">if</span> (!inq[ee.v])  </div><div class="line">                    &#123;  </div><div class="line">                        inq[ee.v] = <span class="literal">true</span>;  </div><div class="line">                        q.push(ee.v);  </div><div class="line">                    &#125;  </div><div class="line">                &#125;  </div><div class="line">                now = pre[now];  </div><div class="line">            &#125;  </div><div class="line">            inq[u] = <span class="literal">false</span>;  </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        <span class="comment">//spfa-t  </span></div><div class="line">        <span class="built_in">memset</span>(inq, <span class="literal">false</span>, <span class="keyword">sizeof</span>(inq));  </div><div class="line">        fill(dt, dt + MAXP, INF);  </div><div class="line">        <span class="keyword">while</span> (!q.empty()) q.pop();  </div><div class="line">        q.push(n); inq[n] = <span class="literal">true</span>; dt[n] = <span class="number">0L</span>L;  </div><div class="line">        <span class="keyword">while</span> (!q.empty())  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">int</span> u = q.front(); q.pop();  </div><div class="line">            <span class="keyword">int</span> now = hd[u];  </div><div class="line">            <span class="keyword">while</span> (now != <span class="number">-1</span>)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">const</span> Edge &amp;ee = e[now];  </div><div class="line">                <span class="keyword">if</span> (dt[u] + ee.t &lt; dt[ee.v])  </div><div class="line">                &#123;  </div><div class="line">                    dt[ee.v] = dt[u] + ee.t;  </div><div class="line">                    <span class="keyword">if</span> (!inq[ee.v])  </div><div class="line">                    &#123;  </div><div class="line">                        inq[ee.v] = <span class="literal">true</span>;  </div><div class="line">                        q.push(ee.v);  </div><div class="line">                    &#125;  </div><div class="line">                &#125;  </div><div class="line">                now = pre[now];  </div><div class="line">            &#125;  </div><div class="line">            inq[u] = <span class="literal">false</span>;  </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        <span class="comment">//find the ans &amp; output  </span></div><div class="line">        <span class="keyword">long</span> <span class="keyword">long</span> ans = INF;  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (max(ds[i], dt[i]) &lt; ans) ans = max(ds[i], dt[i]);  </div><div class="line">        &#125;  </div><div class="line">        ansV.clear();  </div><div class="line">        <span class="keyword">if</span> (ans != INF)  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (max(ds[i], dt[i]) == ans) ansV.push_back(i);  </div><div class="line">        &#125;  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"Case #%d: "</span>, kk);  </div><div class="line">        <span class="keyword">if</span> (ans == INF) <span class="built_in">printf</span>(<span class="string">"Evil John\n"</span>);  </div><div class="line">        <span class="keyword">else</span>  </div><div class="line">        &#123;  </div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%lld\n"</span>, ans);  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; ansV.size(); i++)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">if</span> (i) <span class="built_in">printf</span>(<span class="string">" "</span>);  </div><div class="line">                <span class="built_in">printf</span>(<span class="string">"%d"</span>, ansV[i]);  </div><div class="line">            &#125;  </div><div class="line">            <span class="built_in">printf</span>(<span class="string">"\n"</span>);  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/hdu5536/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/hdu5536/" itemprop="url">hdu 5536 Chip Factory (暴力+优化)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-11-01T00:00:00+08:00">
                2015-11-01
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/hdu5536/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/hdu5536/" class="cy_cmt_count" data-xid="hdu5536/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>暴力+优化<br>话说网上有字典树的正规解法，不过hdu重现赛时并不会，于是直接暴力了。<br>优化方式直接在代码注释中标注了，这里不详解了。</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">1005</span>;  </div><div class="line"><span class="keyword">int</span> a[MAXN];  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">lowbit</span><span class="params">(<span class="keyword">int</span> x)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">return</span> x &amp; -x;  </div><div class="line">&#125;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">int</span> T; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;T);  </div><div class="line">    <span class="keyword">while</span> (T--)  </div><div class="line">    &#123;  </div><div class="line">        <span class="comment">//input  </span></div><div class="line">        <span class="keyword">int</span> n; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)   </div><div class="line">            <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;a[i]);  </div><div class="line">  </div><div class="line">        <span class="comment">//sort  </span></div><div class="line">        sort(a, a + n);  </div><div class="line">  </div><div class="line">        <span class="comment">//main  </span></div><div class="line">        <span class="keyword">int</span> maxValue = a[n<span class="number">-1</span>] + a[n<span class="number">-2</span>]; <span class="comment">//找到最大值  </span></div><div class="line">        <span class="keyword">int</span> maxLimit = maxValue;  </div><div class="line">        <span class="keyword">int</span> ans = <span class="number">0</span>;  </div><div class="line">  </div><div class="line">        <span class="comment">//maxLimit = 小于等于maxValue的最大的2的n次方  </span></div><div class="line">        <span class="comment">//例:若maxValue的二进制表示为11111,则maxLimit为10000  </span></div><div class="line">        <span class="comment">//显然最终的答案一定属于[maxLimit, maxLimit &lt;&lt; 1)  </span></div><div class="line">        <span class="keyword">while</span> (maxLimit - lowbit(maxLimit) &gt; <span class="number">0</span>) maxLimit -= lowbit(maxLimit);   </div><div class="line">  </div><div class="line">        <span class="comment">//第一种情况  </span></div><div class="line">        <span class="comment">//a[i]+a[j]属于[maxLimit, maxLimit &lt;&lt; 1),但a[k]不属于该范围  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = n - <span class="number">1</span>; i &gt; <span class="number">0</span>; i--)  </div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = i - <span class="number">1</span>; j &gt;= <span class="number">0</span>; j--)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (a[i] + a[j] &lt; maxLimit) <span class="keyword">break</span>;  </div><div class="line">            <span class="keyword">int</span> sum = a[i] + a[j];  </div><div class="line">            <span class="keyword">int</span> now = maxLimit, kl, kr, num = <span class="number">0</span>;  </div><div class="line">            <span class="comment">//找到sum的第一个0的位置,该位置满足以下条件:  </span></div><div class="line">            <span class="comment">//1.设这个位置为pos,即(1&lt;&lt;pos) &amp; sum = 0  </span></div><div class="line">            <span class="comment">//2.存在a[k],(k != i &amp;&amp; k != j),a[k]的二进制的首位也在pos这一位  </span></div><div class="line">            <span class="keyword">while</span> (num == <span class="number">0</span> &amp;&amp; now)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">while</span> (now &amp;&amp; (now &amp; sum)) now &gt;&gt;= <span class="number">1</span>;  </div><div class="line">                kl = lower_bound(a, a + n, now) - a;  </div><div class="line">                kr = lower_bound(a, a + n, now &lt;&lt; <span class="number">1</span>) - a;  </div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = kl; k &lt; kr; k++)  </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> (k != i &amp;&amp; k != j)  </div><div class="line">                    &#123;  </div><div class="line">                        num++;  </div><div class="line">                        <span class="keyword">break</span>;  </div><div class="line">                    &#125;  </div><div class="line">                &#125;  </div><div class="line">                now &gt;&gt;= <span class="number">1</span>;  </div><div class="line">            &#125;  </div><div class="line">            <span class="comment">//不存在上述位置的情况  </span></div><div class="line">            <span class="keyword">if</span> (num == <span class="number">0</span>)  </div><div class="line">            &#123;  </div><div class="line">                <span class="comment">//找到最小的位置且k != i &amp;&amp; k != j  </span></div><div class="line">                <span class="keyword">int</span> x = <span class="number">0</span>;  </div><div class="line">                <span class="keyword">if</span> (x == j) x++;  </div><div class="line">                <span class="keyword">if</span> (x == i) x++;  </div><div class="line">                <span class="comment">//如果最小值为0  </span></div><div class="line">                <span class="keyword">if</span> (a[x] == <span class="number">0</span>)  </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> (sum &gt; ans) ans = sum;  </div><div class="line">                &#125;  </div><div class="line">                <span class="keyword">else</span>  </div><div class="line">                &#123;  </div><div class="line">                    <span class="comment">//minLimit = 小于等于a[x]的最大的2的n次方  </span></div><div class="line">                    <span class="comment">//则枚举的范围一定在[minLimit, minLimit &lt;&lt; 1)之间  </span></div><div class="line">                    <span class="keyword">int</span> minLimit = a[x];  </div><div class="line">                    <span class="keyword">while</span> (minLimit - lowbit(minLimit) &gt; <span class="number">0</span>) minLimit -= lowbit(minLimit);  </div><div class="line">                    minLimit &lt;&lt;= <span class="number">1</span>;  </div><div class="line">                    <span class="keyword">for</span> (<span class="keyword">int</span> k = x; k &lt; n &amp;&amp; a[k] &lt; minLimit; k++)  </div><div class="line">                    &#123;  </div><div class="line">                        <span class="keyword">if</span> (k != i &amp;&amp; k != j &amp;&amp; (sum ^ a[k]) &gt; ans) ans = sum ^ a[k];  </div><div class="line">                    &#125;  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">            <span class="keyword">else</span>  </div><div class="line">            &#123;  </div><div class="line">                <span class="comment">//枚举寻找答案  </span></div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> k = kl; k &lt; kr; k++)  </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> (k != i &amp;&amp; k != j &amp;&amp; (sum ^ a[k]) &gt; ans) ans = sum ^ a[k];  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">          </div><div class="line">        <span class="comment">//另外一种情况  </span></div><div class="line">        <span class="comment">//即a[k]属于[maxLimit, maxLimit &lt;&lt; 1),但a[i]+a[j]不属于该范围  </span></div><div class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> k = n - <span class="number">1</span>; k &gt;= <span class="number">0</span>; k--)  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">if</span> (a[k] &lt; maxLimit) <span class="keyword">break</span>;  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; n; i++)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">for</span> (<span class="keyword">int</span> j = i + <span class="number">1</span>; j &lt; n; j++)  </div><div class="line">                &#123;  </div><div class="line">                    <span class="keyword">if</span> (k != i &amp;&amp; k != j &amp;&amp; (a[k] ^ (a[i] + a[j])) &gt; ans) ans = a[k] ^ (a[i] + a[j]);  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
      

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://localhost/sgu482/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="cayun">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/img/head.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="cayun's blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">
                
                <a class="post-title-link" href="/sgu482/" itemprop="url">sgu 482 Impudent Thief (动态规划)</a></h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2015-10-26T00:00:00+08:00">
                2015-10-26
              </time>
            

            

            
          </span>

          

          
            
              <span class="post-comments-count">
              <span class="post-meta-divider">|</span>
              <span class="post-meta-item-icon">
                <i class="fa fa-comment-o"></i>
              </span>
              
                <a href="/sgu482/#SOHUCS" itemprop="discussionUrl">
                  <span id="url::http://localhost/sgu482/" class="cy_cmt_count" data-xid="sgu482/" itemprop="commentsCount" ></span>
                </a>
              
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body han-init-context" itemprop="articleBody">

      
      

      
        
          
            <h1 id="解法"><a href="#解法" class="headerlink" title="解法"></a>解法</h1><p>设$dp<em>{i,j}$为最后一个放$i$，周长为$j$所拿走的木板总高度<br>$dC(i,j)$表示$i$与$j$放在一起，周长的改变量<br>这样dp方程如下<br>$dp</em>{k,j + dC(i, k)} = \max {(dp<em>{i,j} - h</em>{k})} , k &gt; i$<br>时间复杂度为$O(n^2*C)$，即$O(n^3*h)$</p>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><figure class="highlight cpp"><table><tr><td class="code"><pre><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstdio&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;cstring&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;vector&gt;  </span></span></div><div class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;algorithm&gt;  </span></span></div><div class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;  </div><div class="line">  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXN = <span class="number">55</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> MAXC = <span class="number">5210</span>;  </div><div class="line"><span class="keyword">const</span> <span class="keyword">int</span> INF = <span class="number">0x3f3f3f3f</span>;  </div><div class="line"><span class="keyword">int</span> h[MAXN], sum[MAXN], dp[MAXN][MAXC], fat[MAXN][MAXC];  </div><div class="line"><span class="keyword">bool</span> vis[MAXN];  </div><div class="line"><span class="built_in">vector</span>&lt;<span class="keyword">int</span>&gt; ansV;  </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">inline</span> <span class="keyword">int</span> <span class="title">dC</span><span class="params">(<span class="keyword">int</span> u, <span class="keyword">int</span> v)</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="keyword">int</span> res = <span class="number">2</span>;  </div><div class="line">    <span class="keyword">if</span> (h[v] &gt; h[u]) res += <span class="number">2</span> * (h[v] - h[u]);  </div><div class="line">    <span class="keyword">return</span> res;  </div><div class="line">&#125;   </div><div class="line">  </div><div class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span>  </span></div><div class="line"><span class="function"></span>&#123;  </div><div class="line">    <span class="comment">//input  </span></div><div class="line">    h[<span class="number">0</span>] = sum[<span class="number">0</span>] = <span class="number">0</span>;  </div><div class="line">    <span class="keyword">int</span> sumC = <span class="number">0</span>;  </div><div class="line">    ansV.clear();  </div><div class="line">    <span class="built_in">memset</span>(vis, <span class="literal">false</span>, <span class="keyword">sizeof</span>(vis));  </div><div class="line">    <span class="keyword">int</span> n; <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;n);  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="built_in">scanf</span>(<span class="string">"%d"</span>, &amp;h[i]);  </div><div class="line">        sum[i] = sum[i<span class="number">-1</span>] + h[i];  </div><div class="line">        sumC += dC(i - <span class="number">1</span>, i);  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">int</span> mid = (sumC + <span class="number">1</span>) / <span class="number">2</span>;  </div><div class="line">      </div><div class="line">    <span class="comment">//dp initialize  </span></div><div class="line">    <span class="built_in">memset</span>(dp, <span class="number">0</span>, <span class="keyword">sizeof</span>(dp));  </div><div class="line">    <span class="built_in">memset</span>(fat, <span class="number">0</span>, <span class="keyword">sizeof</span>(fat));  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++) dp[i][<span class="number">2</span>*h[i]+<span class="number">2</span>] = sum[n] - h[i];  </div><div class="line">      </div><div class="line">    <span class="comment">//dp  </span></div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; MAXC; j++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">if</span> (dp[i][j])  </div><div class="line">        &#123;  </div><div class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> k = i + <span class="number">1</span>; k &lt;= n; k++)  </div><div class="line">            &#123;  </div><div class="line">                <span class="keyword">if</span> (dp[i][j] - h[k] &gt; dp[k][j + dC(i, k)])  </div><div class="line">                &#123;  </div><div class="line">                    dp[k][j + dC(i, k)] = dp[i][j] - h[k];  </div><div class="line">                    fat[k][j + dC(i, k)] = i;  </div><div class="line">                &#125;  </div><div class="line">            &#125;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">      </div><div class="line">    <span class="comment">//get the biggest value  </span></div><div class="line">    <span class="keyword">int</span> ans = <span class="number">0</span>, ansi = <span class="number">0</span>, ansj = <span class="number">0</span>;  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = mid; j &lt; MAXC; j++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">if</span> (dp[i][j] &gt; ans)  </div><div class="line">        &#123;  </div><div class="line">            ans = dp[i][j];  </div><div class="line">            ansi = i;  </div><div class="line">            ansj = j;  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">      </div><div class="line">    <span class="comment">//output  </span></div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, ans);  </div><div class="line">    <span class="keyword">if</span> (ans == <span class="number">0</span>)  </div><div class="line">    &#123;  </div><div class="line">        <span class="built_in">printf</span>(<span class="string">"0\n"</span>);  </div><div class="line">        <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">int</span> cnt = <span class="number">0</span>;  </div><div class="line">    <span class="keyword">while</span> (ansi)  </div><div class="line">    &#123;  </div><div class="line">        vis[ansi] = <span class="literal">true</span>; cnt++;  </div><div class="line">        <span class="keyword">int</span> ti = fat[ansi][ansj];  </div><div class="line">        <span class="keyword">int</span> tj = ansj - dC(ti, ansi);  </div><div class="line">        ansi = ti;  </div><div class="line">        ansj = tj;  </div><div class="line">    &#125;  </div><div class="line">    <span class="built_in">printf</span>(<span class="string">"%d\n"</span>, n - cnt);  </div><div class="line">    <span class="keyword">bool</span> flag = <span class="literal">false</span>;  </div><div class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= n; i++)  </div><div class="line">    &#123;  </div><div class="line">        <span class="keyword">if</span> (!vis[i])  </div><div class="line">        &#123;  </div><div class="line">            flag ? <span class="built_in">printf</span>(<span class="string">" "</span>) : flag = <span class="literal">true</span>;  </div><div class="line">            <span class="built_in">printf</span>(<span class="string">"%d"</span>, i);  </div><div class="line">        &#125;  </div><div class="line">    &#125;  </div><div class="line">    <span class="built_in">printf</span>(<span class="string">"\n"</span>);  </div><div class="line">      </div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;  </div><div class="line">&#125;</div></pre></td></tr></table></figure>
          
        
      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      

      

      
      
        <div class="post-eof"></div>
      
    </footer>
  </div>
  
  
  
  </article>


    
  </section>

  
  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/page/9/"><i class="fa fa-angle-left"></i></a><a class="page-number" href="/">1</a><span class="space">&hellip;</span><a class="page-number" href="/page/9/">9</a><span class="page-number current">10</span><a class="page-number" href="/page/11/">11</a><a class="extend next" rel="next" href="/page/11/"><i class="fa fa-angle-right"></i></a>
  </nav>



          </div>
          


          

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/img/head.png"
                alt="cayun" />
            
              <p class="site-author-name" itemprop="name">cayun</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">102</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">79</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/comeacrossyun" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-github"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="mailto:comeacrossyun@gmail.com" target="_blank" title="E-Mail">
                    
                      <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">cayun</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Pisces</a></div>




        







        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  

  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  




	





  





  




  
    <script id="cy_cmt_num" src="https://changyan.sohu.com/upload/plugins/plugins.list.count.js?clientId=cyt6VLWhj"></script>
  









  





  

  
<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>


  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
